import pandas as pd
from matplotlib import pyplot as plt
import requests
import json
import time
import requests.cookies

# 1. 登录网易有道词典的PC版，微信扫码登录，复制cookies到文件
def youdao_demo():
    cookie_jar = requests.cookies.RequestsCookieJar()
    with open("../course_datas/c32_read_html/cookie.txt") as fin:
        cookiejson = json.loads(fin.read())
    for cookie in cookiejson:
        cookie_jar.set(
            name=cookie["name"],
            value=cookie["value"],
            domain=cookie["domain"],
            path=cookie["path"]
        )
    # 2. 将html都下载下来存入列表
    htmls = []
    url = "http://dict.youdao.com/wordbook/wordlist?p={idx}&tags="
    for index in range(6):
        time.sleep(1)
        print("**爬数据：第%d页" % index)
        res = requests.get(url.format(idx=index),cookies=cookie_jar)
        htmls.append(res)
        print(url.format(idx=index))
if __name__ == "__main__":
    youdao_demo()
